﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace BaiTap1
{
    public partial class frmMainForm : Form
    {
        KhachHang_BUS khachhang;
        XeMay_BUS xemay;

        DataTable tableKH;
        DataTable tableXe;
        public frmMainForm()
        {
            InitializeComponent();
        }
       

        public void AddBindingKhachHang(DataTable table)
        {
            dgvKhachHang.DataSource = table;
            txtTen.DataBindings.Add("TEXT", table, "TenKH");
            txtMaKH.DataBindings.Add("TEXT", table, "MaKH");
            txtHo.DataBindings.Add("text", table, "HoKH");
            txtTenLot.DataBindings.Add("text", table, "Tenlot");
            txtDiaChi.DataBindings.Add("text", table, "Diachi");
            cmbPhai.DataBindings.Add("text", table, "Phai");
            txtSDT.DataBindings.Add("text", table, "Sdt");
        }

        public void AddBindingXeMay(DataTable table)
        {
            dgvHienThiDanhSachXe.DataSource = table;
            txtSoXe.DataBindings.Add("text", table, "soxe");
            txtMauXe.DataBindings.Add("text", table, "mauxe");
            txtXuatXu.DataBindings.Add("text", table, "nuocsx");
            txtKhachHang.DataBindings.Add("text", table, "makh");
        }

        public void ClearBindingKhachHang()
        {
            txtDiaChi.DataBindings.Clear();
            txtHo.DataBindings.Clear();
            txtMaKH.DataBindings.Clear();
            txtSDT.DataBindings.Clear();
            txtTen.DataBindings.Clear();
            cmbPhai.DataBindings.Clear();
            txtTenLot.DataBindings.Clear();
        }
        
        public void ClearBindingXeMay()
        {
            txtSoXe.DataBindings.Clear();
            txtMauXe.DataBindings.Clear();
            txtXuatXu.DataBindings.Clear();
            txtKhachHang.DataBindings.Clear();
        }

        private void frmMainForm_Load(object sender, EventArgs e)
        {
            cmbPhai.Items.Add("F");
            cmbPhai.Items.Add("M");

            rdbTimTheoMaKH.Checked = true;
            
            khachhang = new KhachHang_BUS();
            tableKH = khachhang.LayDanhSachKhachHang();

            xemay=new XeMay_BUS();
            tableXe=xemay.LayDanhSachXeMay();
            
            AddBindingKhachHang(tableKH);
            AddBindingXeMay(tableXe);


            
            //dgvKhachHang.DataSource = table;
            //txtTen.DataBindings.Add("TEXT", table, "TenKH");
            //txtMaKH.DataBindings.Add("TEXT", table, "MaKH");
            //txtHo.DataBindings.Add("text", table, "HoKH");
            //txtTenLot.DataBindings.Add("text", table, "Tenlot");
            //txtDiaChi.DataBindings.Add("text", table, "Diachi");
            //cmbPhai.DataBindings.Add("text", table, "Phai");
            //txtSDT.DataBindings.Add("text", table, "Sdt");
        }

        private void btnHienThiDanhSachKH_Click(object sender, EventArgs e)
        {
            ClearBindingKhachHang();
            //txtDiaChi.DataBindings.Clear();
            //txtHo.DataBindings.Clear();
            //txtMaKH.DataBindings.Clear();
            //txtSDT.DataBindings.Clear();
            //txtTen.DataBindings.Clear();
            //cmbPhai.DataBindings.Clear();
            //txtTenLot.DataBindings.Clear();
            khachhang = new KhachHang_BUS();
            tableKH = khachhang.LayDanhSachKhachHang();
            AddBindingKhachHang(tableKH);
            //dgvKhachHang.DataSource = table;
            //txtTen.DataBindings.Add("TEXT", table, "TenKH");
            //txtMaKH.DataBindings.Add("TEXT", table, "MaKH");
            //txtHo.DataBindings.Add("text", table, "HoKH");
            //txtTenLot.DataBindings.Add("text", table, "Tenlot");
            //txtDiaChi.DataBindings.Add("text", table, "Diachi");
            //cmbPhai.DataBindings.Add("text", table, "Phai");
            //txtSDT.DataBindings.Add("text", table, "Sdt");
        }

        private void btnThemKH_Click(object sender, EventArgs e)
        {
            KhachHang_DTO kh = new KhachHang_DTO(txtMaKH.Text, txtHo.Text, txtTenLot.Text, txtTen.Text, txtDiaChi.Text, cmbPhai.SelectedItem.ToString(), txtSDT.Text);
            khachhang = new KhachHang_BUS();
            if (khachhang.ThemKhachHang(kh))
            {
                //MessageBox.Show("Them thanh cong!");
                tableKH = khachhang.LayDanhSachKhachHang();
                dgvKhachHang.DataSource = tableKH;

                ClearBindingKhachHang();
                //txtDiaChi.DataBindings.Clear();
                //txtHo.DataBindings.Clear();
                //txtMaKH.DataBindings.Clear();
                //txtSDT.DataBindings.Clear();
                //txtTen.DataBindings.Clear();
                //cmbPhai.DataBindings.Clear();
                //txtTenLot.DataBindings.Clear();
                AddBindingKhachHang(tableKH);
                //txtTen.DataBindings.Add("TEXT", table, "TenKH");
                //txtMaKH.DataBindings.Add("TEXT", table, "MaKH");
                //txtHo.DataBindings.Add("text", table, "HoKH");
                //txtTenLot.DataBindings.Add("text", table, "Tenlot");
                //txtDiaChi.DataBindings.Add("text", table, "Diachi");
                //cmbPhai.DataBindings.Add("text", table, "Phai");
                //txtSDT.DataBindings.Add("text", table, "Sdt");
            }
            else
                MessageBox.Show("Them khong thanh cong!");
        }

        private void bntXoaKH_Click(object sender, EventArgs e)
        {
            KhachHang_DTO kh = new KhachHang_DTO(txtMaKH.Text, txtHo.Text, txtTenLot.Text, txtTen.Text, txtDiaChi.Text, cmbPhai.SelectedItem.ToString(), txtSDT.Text);
            khachhang = new KhachHang_BUS();
            if (khachhang.XoaKhachHang(kh))
            {
                MessageBox.Show("Xoa thanh cong!");
                tableKH = khachhang.LayDanhSachKhachHang();
                dgvKhachHang.DataSource = tableKH;
                ClearBindingKhachHang();

                //txtDiaChi.DataBindings.Clear();
                //txtHo.DataBindings.Clear();
                //txtMaKH.DataBindings.Clear();
                //txtSDT.DataBindings.Clear();
                //txtTen.DataBindings.Clear();
                //cmbPhai.DataBindings.Clear();
                //txtTenLot.DataBindings.Clear();
                AddBindingKhachHang(tableKH);
                //txtTen.DataBindings.Add("TEXT", table, "TenKH");
                //txtMaKH.DataBindings.Add("TEXT", table, "MaKH");
                //txtHo.DataBindings.Add("text", table, "HoKH");
                //txtTenLot.DataBindings.Add("text", table, "Tenlot");
                //txtDiaChi.DataBindings.Add("text", table, "Diachi");
                //cmbPhai.DataBindings.Add("text", table, "Phai");
                //txtSDT.DataBindings.Add("text", table, "Sdt");
            }
            else
                MessageBox.Show("Xoa khong thanh cong!");
        }

        private void btnSuaKH_Click(object sender, EventArgs e)
        {
            KhachHang_DTO kh = new KhachHang_DTO(txtMaKH.Text, txtHo.Text, txtTenLot.Text, txtTen.Text, txtDiaChi.Text, cmbPhai.SelectedItem.ToString(), txtSDT.Text);
            khachhang = new KhachHang_BUS();
            if (khachhang.SuaKhachHang(kh))
            {
                MessageBox.Show("Sua thanh cong!");
                tableKH = khachhang.LayDanhSachKhachHang();
                dgvKhachHang.DataSource = tableKH;
                ClearBindingKhachHang();
                //txtDiaChi.DataBindings.Clear();
                //txtHo.DataBindings.Clear();
                //txtMaKH.DataBindings.Clear();
                //txtSDT.DataBindings.Clear();
                //txtTen.DataBindings.Clear();
                //cmbPhai.DataBindings.Clear();
                //txtTenLot.DataBindings.Clear();
                AddBindingKhachHang(tableKH);
                //txtTen.DataBindings.Add("TEXT", table, "TenKH");
                //txtMaKH.DataBindings.Add("TEXT", table, "MaKH");
                //txtHo.DataBindings.Add("text", table, "HoKH");
                //txtTenLot.DataBindings.Add("text", table, "Tenlot");
                //txtDiaChi.DataBindings.Add("text", table, "Diachi");
                //cmbPhai.DataBindings.Add("text", table, "Phai");
                //txtSDT.DataBindings.Add("text", table, "Sdt");
            }
            else
                MessageBox.Show("Sua khong thanh cong!");
        }

        private void btnTimKH_Click(object sender, EventArgs e)
        {
            if (rdbTimTheoMaKH.Checked)
            {
                khachhang = new KhachHang_BUS();
                DataTable table = khachhang.TimKiemTheoMaKhachHang(txtKeyWork.Text);
                dgvKhachHang.DataSource = table;
            }
            else if (rdbTimTheoTen.Checked)
            {
                khachhang = new KhachHang_BUS();
                DataTable table = new DataTable();
                table = khachhang.TimKiemTheoTenKhachHang(txtKeyWork.Text);
                dgvKhachHang.DataSource = table;
            }
            else
                return;
        }

        private void btnHienThiDanhSachXe_Click(object sender, EventArgs e)
        {
            xemay=new XeMay_BUS();
            DataTable tb = xemay.LayDanhSachXeMay();
            ClearBindingXeMay();
            AddBindingXeMay(tb);
        }

        private void btnTimKiemXe_Click(object sender, EventArgs e)
        {
            if(txtTimKiemXe.Text!="")
            {
                xemay=new XeMay_BUS();
                DataTable tb = xemay.TimKiem(txtTimKiemXe.Text);
                ClearBindingXeMay();
                AddBindingXeMay(tb);
            }
            else
                return;
        }
        
    }
}
